package com.grace.project.common.util.spring;

import java.util.Locale;
import java.util.Map;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

/**
 * 多视图控制器
 * @author Administrator
 *
 */
public class MultiViewResover implements ViewResolver{

	private Map<String,ViewResolver> resolvers;
	
	@Override
	public View resolveViewName(String viewName, Locale locale)
			throws Exception {
		
		int n = viewName.lastIndexOf("."); // 获取viewName(modelAndView中的名字)看其有没有.
		//没有默认是jsp视图
		if(n != -1){  
            //取出扩展名
            String suffix = viewName.substring(n + 1);  
            //取出对应的ViewResolver  
            ViewResolver resolver=resolvers.get(suffix);
            if(resolver==null){  
                throw new RuntimeException("No ViewResolver for "+suffix);
            }  
            return resolver.resolveViewName(viewName, locale);
        }else{  
            ViewResolver resolver = resolvers.get("jsp");
            return resolver.resolveViewName(viewName, locale);
        }  
	}

	public Map<String, ViewResolver> getResolvers() { 
        return resolvers; 
    } 
 
    public void setResolvers(Map<String, ViewResolver> resolvers) { 
        this.resolvers = resolvers; 
    } 
}
